//
//  JsMessageReminderView.m
//  JsUtilitySdk
//
//  Created by GikkiAres on 2020/5/27.
//  Copyright © 2020 base. All rights reserved.
//

#import "JsDisplayManager.h"
#import "JsGeometryManager.h"
#import "JsBundleManager.h"
#import "JsMessageReminderView.h"

@interface JsMessageReminderView()
{
	UILabel * mlb;
	UIImageView * miv;
	UITapGestureRecognizer * mTap;
}

@end


@implementation JsMessageReminderView

#pragma mark 1 LifeCircle

- (instancetype)init {
	if(self = [super init]) {
		[self commonInit];
	}
	return self;
}

- (instancetype)initWithFrame:(CGRect)frame {
	if (self = [super initWithFrame:frame]) {
		[self commonInit];
	}
	return self;
}



- (instancetype)initWithVm:(JsMessageReminderViewVm *)vm {
	if(self = [super initWithVm:vm]) {
		[self commonInit];
	}
	return self;
}

- (instancetype)initWithMessage:(NSString *)message {
	return [self initWithMessage:message image:nil];
}
- (instancetype)initWithMessage:(NSString *)message image:(UIImage * _Nullable)image {
	JsMessageReminderViewVm * vm = [JsMessageReminderViewVm vmWithTitle:message contentType:JsMessageReminderContentTypeSilent arrangeType:JsMessageReminderArrangeTypeHorizontal colorStyle:JsMessageReminderColorTypeColor];
	return [self initWithVm:vm];
}

- (instancetype)initWithMessage:(NSString *)message contentType:(JsMessageReminderContentType)contentType {
	JsMessageReminderViewVm * vm = [JsMessageReminderViewVm vmWithTitle:message contentType:contentType arrangeType:JsMessageReminderArrangeTypeHorizontal colorStyle:JsMessageReminderColorTypeColor];
	
	return [self initWithVm:vm];
}


- (void) commonInit {
}

#pragma mark InnerFunc
- (void)setImageInset:(UIEdgeInsets)insets {
		//	__weak typeof(self)weakSelf = self;
		//	positonBlock = ^{
		//		[weakSelf doPositionWithImageInset:insets];
		//	};
}

	//- (void)doPositionWithImageInset:(UIEdgeInsets)insets {
	//	CGFloat imageSize = 30;
	//	CGFloat x_image = (self.bounds.size.width - imageSize)/2;
	//	CGFloat y_iamge = insets.top;
	//	self.imageView.frame = CGRectMake(x_image, y_iamge, imageSize, imageSize);
	//
	//	CGFloat spacing = 20;
	//	CGFloat w_label = [self.label sizeThatFits:CGSizeMake(0, 0)].width;
	//	CGFloat h_label = [self.label sizeThatFits:CGSizeMake(0, 0)].height;
	//	CGFloat x_label = (self.bounds.size.width - w_label)/2;
	//	CGFloat y_label = insets.top+imageSize+spacing;
	//	self.imageView.frame = CGRectMake(x_label, y_label, w_label, h_label);
	//}

/**
 有的时候,label没有准备好,就layoutSubviews了??
 */

- (void)layoutSubviews {
	[super layoutSubviews];
	CGSize selfSize = self.bounds.size;
	if(!self.vm.image&&self.vm.title) {
			//仅仅显示文字
//		float superWidth = self.superview.frame.size.width;
//		float margin = 20;
//		float padding = 10;
//			//		float width = MIN(superWidth - margin * 20,oneLineTextSize.width + padding * 2);
//		CGSize textSize = [JsGeometryManager sizeForMultiLineStringWithContext:self.vm.title font:self.vm.font maxWidth:superWidth - margin * 2 - padding *2 maxHeight:0];
//
//		self.bounds = CGRectMake(0, 0, textSize.width + padding *2, textSize.height + padding * 2);
//		CGRect frame = [self calculateTargetFrame];
//		self.frame = frame;
			//		[self setMessage:self.vm.title];
		mlb.frame =  [JsGeometryManager frameHAlignCenterVAlignCenterWithContainerSize:selfSize size:self.vm.titleSize];
//		mlb.textColor = [UIColor blackColor];
	}
	else {
		//显示文字和label
		
			if(self.vm.arrangeStyle == 1) {
				CGRect frame_iv = [JsGeometryManager frameHAlignLeftVAlignCenterWithContainerSize:selfSize size:self.vm.imageSize offset:CGPointMake(self.vm.padding, 0)];
				miv.frame = frame_iv;
				mlb.frame = [JsGeometryManager frameHRightVAlignCenterWithContainerSize:selfSize size:self.vm.titleSize siblingFrame:frame_iv offset:CGPointMake(self.vm.padding, 0)];
			}
			else {
				CGRect imageViewFrame = [JsGeometryManager frameHAlignCenterVAlignTopWithContainerSize:selfSize size:self.vm.imageSize offset:CGPointMake(0, self.vm.mf_imageViewTop)];
				miv.frame = imageViewFrame;
				if(mlb) {
					CGSize textSize = [JsGeometryManager sizeForMultiLineStringWithContext:self.vm.title font:self.vm.font maxWidth:selfSize.width-20];
					CGRect labelFrame = [JsGeometryManager frameHAlignCenterVBelowWithContainerSize:selfSize size:textSize siblingFrame:imageViewFrame offset:CGPointMake(0, self.vm.mf_labelTop)];
					mlb.frame = labelFrame;
					mlb.textColor = self.vm.mcolor_message;
					mlb.text = self.vm.title;
					mlb.font = self.vm.font;
				}
			}
	}
}

#pragma mark 2 InnerFunction
- (void)setVm:(JsMessageReminderViewVm *)vm {
	vm = vm;
	[super setVm:vm];
}


- (void)updateSelfSize {
	CGRect frame = self.frame;
	float _width = self.frame.size.width;
	float _height = self.frame.size.height;
	//计算titleSize
	if(self.vm.maxWidth > 0) {
		float maxTitleWidth = 0;
		if(self.vm.arrangeStyle == JsMessageReminderArrangeTypeHorizontal) {
			if(self.vm.image) {
				maxTitleWidth = self.vm.maxWidth - self.vm.padding * 2 - self.vm.ivLbSpacing - self.vm.imageSize.width;
			}
			else {
				maxTitleWidth = self.vm.maxWidth - self.vm.padding * 2;
			}
		}
		else {
				maxTitleWidth = self.vm.maxWidth - self.vm.padding * 2;
		}
		self.vm.titleSize = [JsGeometryManager sizeForMultiLineStringWithContext:self.vm.title font:self.vm.font maxWidth:maxTitleWidth];
	}
	else {
		self.vm.titleSize = [JsGeometryManager sizeForOneLineStringWithContext:self.vm.title font:self.vm.font];
	}
	//计算selfWidth
	if(self.vm.dWidth > 0) {
		_width = self.vm.dWidth;
	}
	else if(self.vm.dWidth == WRAP_CONTENT) {
		_width = self.vm.padding * 2;
		_width += self.vm.titleSize.width;
		if(self.vm.image && self.vm.arrangeStyle == JsMessageReminderArrangeTypeHorizontal) {
			_width += self.vm.ivLbSpacing;
			_width += self.vm.imageSize.width;
		}
	}
	
	if(self.vm.dHeight > 0) {
		_height = self.vm.dHeight;
	}
	else if(self.vm.dHeight == WRAP_CONTENT) {
		_height = self.vm.padding * 2;
		if(self.vm.arrangeStyle == JsMessageReminderArrangeTypeHorizontal) {
			if(self.vm.image) {
				_height += MAX(self.vm.titleSize.height,self.vm.imageSize.height);
			}
			else {
				_height += self.vm.titleSize.height;
			}
		}
		else {
			//竖直排列
			if(self.vm.image) {
				_height += self.vm.titleSize.height + self.vm.imageSize.height + self.vm.ivLbSpacing;
			}
			else {
				_height += self.vm.titleSize.height;
			}
		}
	}
	frame.size.width = _width;
	frame.size.height = _height;
	self.frame = frame;
}

- (void)renderVm {
	[super renderVm];
	[self updateSelfSize];

	
		//	self.vm.mfont = font;
	if(!mlb) {
		mlb = [[UILabel alloc]init];
		mlb.textAlignment = NSTextAlignmentCenter;
		mlb.numberOfLines = 0;
		[self addSubview:mlb];
	}
	mlb.text = self.vm.title;
	mlb.font = self.vm.font;
	mlb.textColor = self.vm.titleColor;
	
	if(self.vm.image && !miv) {
		miv = [[UIImageView alloc]init];
		[self addSubview:miv];
	}
	miv.image = self.vm.image;
	[self layoutIfNeeded];
}

#pragma mark 3 Interface
- (void)setImage:(UIImage *)image {
	miv.image = image;
	self.vm.image = image;
}
- (void)setMessage:(NSString *)message {
	[self setMessage:message font:self.vm.font];
}
- (void)setMessage:(NSString *)message font:(UIFont *)font {
	self.vm.title = message;
	self.vm.font = font;
	[self updateVm];
}

#pragma mark 4 Delegate

#pragma mark 5 Event
- (void)dealloc {
		//	NSLog(@"JsMessageReminderView dealloc");
}

- (void)onTap:(UITapGestureRecognizer *)tap {
	[self undisplay];
}

@end
